package com.example.demo;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.AuthorizationScope;
import springfox.documentation.service.SecurityReference;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * Copyright © 合生活. All rights reserved.
 *
 * @Title: Swagger 参数  文档 https://www.jianshu.com/p/e46bf50a5ab3
 * @Prject: membership-service
 * @Package: com.membership.service
 * @Description: ${DESCRIPTION}m
 * @author: feejson
 * @date: 2018/7/6 11:48
 * @version: V1.0
 */
@Configuration
@EnableSwagger2
public class Swagger2Config {
    @Bean
    public Docket createRestApi() {
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .enable(true)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.example.demo.controller"))
                .paths(PathSelectors.regex("^(?!lg).*$"))
                .paths(PathSelectors.any())
                .build();
        docket.securitySchemes(securitySchemes());
        docket.securityContexts(securityContexts());
        return docket;
    }

    /**
     * 自定义访问Key header
     * @return
     */
    private List<ApiKey> securitySchemes() {
        List<ApiKey> ls  = new ArrayList<>();
        ls.add(new ApiKey("Authorization", "HOPSON_SESSION_ID", "header"));
        ls.add(new ApiKey("Authorization1", "USER_ID", "header"));
        ls.add(new ApiKey("Authorization2", "TOKEN", "header"));
        ls.add(new ApiKey("Authorization3", "CLIENT_TYPE", "header"));
        ls.add(new ApiKey("Authorization4", "GROUP_ID", "header"));
        return ls;
    }
    private List<SecurityContext> securityContexts() {
        List<SecurityContext> ls = new ArrayList<>();
        ls.add(SecurityContext.builder()
                .securityReferences(defaultAuth())
                .forPaths(PathSelectors.regex("^(?!login).*$"))
                .build());
        return ls;
    }
    List<SecurityReference> defaultAuth() {
        List<SecurityReference> ls = new ArrayList<>();
        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = authorizationScope;
        ls.add(new SecurityReference("Authorization", authorizationScopes));
        ls.add(new SecurityReference("Authorization1", authorizationScopes));
        ls.add(new SecurityReference("Authorization2", authorizationScopes));
        ls.add(new SecurityReference("Authorization3", authorizationScopes));
        ls.add(new SecurityReference("Authorization4", authorizationScopes));
        return ls;
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("接口文档")
                .description("提供详细的后台所有Restful接口")
                .termsOfServiceUrl("http://127.0.0.1:8088/")
                .contact("feejson")
                .version("1.0")
                .build();
    }
}